import { useMutation, useQueryClient } from '@umijs/max';
import { request } from '../axios';
import APP_PRE from '../constant/app-pre';
import QUERY_KEYS from '../constant';

const prefix = `${APP_PRE.APP_NAME}/like`;

const useDeleteLike = () => {
  const query = useQueryClient();
  return useMutation(
    (like: API.ILike) =>
      request(`${prefix}/delete`, {
        method: 'POST',
        params: like,
      }),
    {
      onSuccess: (res: API.IResult<API.IInsertId>) => {
        if (res.code === 200) {
          query.invalidateQueries([QUERY_KEYS.COMMENT_GET_ALL_BY_AID]);
        }
      },
    },
  );
};

const useUpdateLike = () => {
  const query = useQueryClient();
  return useMutation(
    (like: API.ILike) =>
      request(`${prefix}/update`, {
        method: 'POST',
        data: like,
      }),
    {
      onSuccess: (res: API.IResult<API.IInsertId>) => {
        if (res.code === 200) {
          query.invalidateQueries([QUERY_KEYS.COMMENT_GET_ALL_BY_AID]);
        }
      },
    },
  );
};

export { useDeleteLike, useUpdateLike };
